package com.evan.common.dao;

import com.evan.common.bit.ZkServerModeEnum;
import com.evan.common.bit.ZkServerStatus;
import com.evan.common.pojo.ZkServerInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface ZkServerDao extends JpaRepository<ZkServerInfo,Integer> {

    @Query("from ZkServerInfo t0 where t0.clusterId = :clusterId")
    List<ZkServerInfo> findAllByClusterId(@Param("clusterId") int clusterId);

    @Transactional
    @Modifying
    @Query("update ZkServerInfo t0 " +
            "set t0.serverMode = :zkMode,t0.serverStatus = :stat " +
            "where t0.zid = :zid")
    void updateZkStat(@Param("zid") Integer zid,
                      @Param("zkMode") ZkServerModeEnum serverMode,
                      @Param("stat") ZkServerStatus stat);
}
